<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/28
 * Time: 14:15
 * Auth: YWH
 */

namespace App\Http\Controllers\Api\Machine;


use App\Http\Controllers\ApiController;
use App\Models\MachineGoodsModel;
use App\Models\MachineModel;
use Illuminate\Http\Request;

class MachineAndGoodsController extends ApiController
{

    public function getMachineGoods(Request $request)
    {
        $req = $request->all();
        if (empty($req['machine_id'])){
            return self::response([],'-1','machine_id is null');
        }
        $MachineGoodsModel = new MachineGoodsModel();
        $data              = $MachineGoodsModel->getGoodsListByMachineId($req['machine_id']);
        return self::response($data);
    }

    public function delMachineFromHotel(Request $request)
    {
        $req = $request->all();
        if (empty($req['machine_id'])){
            return self::response([],'-1','machine_id is null');
        }
        $MachineGoodsModel = new MachineGoodsModel();
        $flag              = $MachineGoodsModel->delMachineFromHotel($req['machine_id']);
        if ($flag !==false){
            return self::response([]);
        }else{
            return self::response([],-5,'操作失败');
        }
    }

    public function getHotelList(Request $request)
    {
        $page = $request->input('page');
        $machineList    = MachineModel::getMachineList($page);
        return ApiController::response($machineList);
    }

    public function searchMachine(Request $request)
    {
        $req  = $request->all();
        $page = $request->input('page');
        $machineList    = MachineModel::searchMachineList($req,$page);
        return self::response($machineList);
    }

    /**设备的添加或编辑
     * @param Request $request
     * @return mixed
     * @throws \ErrorException
     */
    public function saveMachine(Request $request)
    {
        if(!MachineModel::save_machine($request->all())){
            return self::response('',self::ERROR_CODE,(new MachineModel())->error);
        }
        return self::response('');
    }

    /**设置格子有货。无货
     * @param Request $request
     * @return mixed
     * @throws \ErrorException
     */
    public function settingBoxStatus(Request $request)
    {
        if(!MachineGoodsModel::setting_box_status($request->all())){
            return self::response('',self::ERROR_CODE,MachineGoodsModel::$error);
        }
        return self::response('');
    }



}